﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * By replacing the 1st digit of *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
     * */
    class Problem51 : IProblem
    {
        public string Calculate()
        {
            //taktika: nac primeove, i to one koje imaju znamenku najviše 9-n+1, gdje je n broj primeova koje tražimo
            //za svaku od takvih znamenki - istih, pokušat povećavat za jedan (pojedinačno, i grupno) i gledat jesu li prime
            //   za svaki prime counter++
            //   kad je gotovo, counter = 0; i slijedeći prime

            int n = 8;

            int number = 1;


            while (true)
            {
                number += 2; //nema smisla gledat parne

                if (CommonFunctions.IsPrime(number))
                {
                    int maxDigit = 9 - n + 1;

                    //za svaku koja je manja pojedinačno. za zadnju znamenku ne, jer parni nisu uključeni!
                    var digits = CommonFunctions.GetDigits((long)number);

                    Dictionary<int, List<int>> digitPowers = new Dictionary<int, List<int>>();

                    int power = 0;

                    foreach (int digit in digits)
                    {
                        if (power > 0) //negledamo prvu
                        {
                            if (digit <= maxDigit)
                            {
                                if (!digitPowers.ContainsKey(digit))
                                {
                                    digitPowers[digit] = new List<int>();
                                    digitPowers[digit].Add(power);
                                }
                                else
                                {
                                    digitPowers[digit].Add(power);
                                }
                            }
                        }
                        power++;
                    }

                    bool found = false;

                    foreach (int digit in digitPowers.Keys)
                    {
                        if (Test(number, digitPowers[digit], digit, n))
                        {
                            found = true;
                            break;
                        }
                    }

                    if (found)
                        break;
                }
            }

            return number.ToString();
        }

        /// <summary>
        /// Za nać odgovarajućeg
        /// </summary>
        /// <param name="prime">Prim broj koji gledamo</param>
        /// <param name="powers">Pozicije brojeva koje povećavamo za jedan</param>
        /// <param name="startingNumber">Broj s kojim počinjemo</param>
        /// <param name="n">Broj traženih</param>
        /// <returns></returns>
        bool Test(int prime, List<int> powers, int startingNumber, int n)
        {
            int counter = 1;
            int slack = 9 - n + 1 - startingNumber;

            while (counter < n)
            {
                if (slack < 0) //znači ako već sad nemožemo stić
                    return false;

                foreach (int power in powers)
                {
                    prime += (int)Math.Pow(10, power); //dodajemo na tim potencijama za jedan
                }

                if (CommonFunctions.IsPrime(prime))
                {
                    counter++;
                }
                else
                {
                    slack--;
                }
            }

            //counter je jednak n
            return true;
        }
    }
}
